﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.AuthUtility;
using System.Data;
using Newtonsoft.Json;
using XWG.Framework.MSException;

namespace XWG.JWBST.JWBSTManagementApp.AjaxServer.Statistics
{
    /// <summary>
    /// BjztStatistics 的摘要说明
    /// </summary>
    public class BjztStatistics : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {
        protected void BjztStatistic()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            //string orgna_id = context.Request.Form["orgna_id"];
            string kssj = context.Request.Form["texTimeStart"]; //开始时间
            string jssj = context.Request.Form["texTimeEnd"]; //结束时间
            string strSql = string.Empty;
            if (!string.IsNullOrEmpty(kssj) && string.IsNullOrEmpty(jssj))
            {
                strSql = string.Format("select  replyunitcode dwdm,orgna_jc dwmc,sum(decode(bjsj,1,sl,0)) ksbl,sum(decode(bjsj,2,sl,0)) zcbl,sum(decode(sign(bjsj-2),1,sl,0)) cqbl from (select replyunitcode,orgna_jc,bjsj,count(*) sl from (select t.replyunitcode, o.orgna_jc, t.replydate, t.bjsj from TB_BST_ONLINE_ASK t inner join App_Organization o on t.replyunitcode = o.orgna_code where to_char(t.createdate,'yyyy-MM-dd')>='{0}' and t.replysign = '1' and t.replyunitcode is not null) group by replyunitcode,orgna_jc,bjsj) group by replyunitcode,orgna_jc",kssj);
            }   
            else if (string.IsNullOrEmpty(kssj) && !string.IsNullOrEmpty(jssj))
            {
                strSql = string.Format("select  replyunitcode dwdm,orgna_jc dwmc,sum(decode(bjsj,1,sl,0)) ksbl,sum(decode(bjsj,2,sl,0)) zcbl,sum(decode(sign(bjsj-2),1,sl,0)) cqbl from (select replyunitcode,orgna_jc,bjsj,count(*) sl from (select t.replyunitcode, o.orgna_jc, t.replydate, t.bjsj from TB_BST_ONLINE_ASK t inner join App_Organization o on t.replyunitcode = o.orgna_code where to_char(t.createdate,'yyyy-MM-dd')<='{0}' and t.replysign = '1' and t.replyunitcode is not null) group by replyunitcode,orgna_jc,bjsj) group by replyunitcode,orgna_jc",jssj);
            }
            else if (!string.IsNullOrEmpty(kssj) && !string.IsNullOrEmpty(jssj))
            {
                strSql = string.Format("select  replyunitcode dwdm,orgna_jc dwmc,sum(decode(bjsj,1,sl,0)) ksbl,sum(decode(bjsj,2,sl,0)) zcbl,sum(decode(sign(bjsj-2),1,sl,0)) cqbl from (select replyunitcode,orgna_jc,bjsj,count(*) sl from (select t.replyunitcode, o.orgna_jc, t.replydate, t.bjsj from TB_BST_ONLINE_ASK t inner join App_Organization o on t.replyunitcode = o.orgna_code where to_char(t.createdate,'yyyy-MM-dd')>='{0}' and to_char(t.createdate,'yyyy-MM-dd')<='{1}' and t.replysign = '1' and t.replyunitcode is not null) group by replyunitcode,orgna_jc,bjsj) group by replyunitcode,orgna_jc", kssj, jssj);
            }
            else
            {
                strSql = string.Format("select  replyunitcode dwdm,orgna_jc dwmc,sum(decode(bjsj,1,sl,0)) ksbl,sum(decode(bjsj,2,sl,0)) zcbl,sum(decode(sign(bjsj-2),1,sl,0)) cqbl from (select replyunitcode,orgna_jc,bjsj,count(*) sl from (select t.replyunitcode, o.orgna_jc, t.replydate, t.bjsj from TB_BST_ONLINE_ASK t inner join App_Organization o on t.replyunitcode = o.orgna_code where  t.replysign = '1' and t.replyunitcode is not null) group by replyunitcode,orgna_jc,bjsj) group by replyunitcode,orgna_jc");
            }
             
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dt = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            try
            {
                dt = Q.GetRecordByPage("DWDM,DWMC,KSBL,ZCBL,CQBL", string.Empty, "DWDM", intPageSize, intPageIndex, strSql, "", "desc").Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        hm = new HearderMessage();
                        hm.name = dt.Columns[i].Caption;
                        list.Add(hm);
                    }
                    tm.Header = list;
                    tm.data = dt;
                    tm.totalCount = Convert.ToString((int)Q.RowCount);
                }
                string json = JsonConvert.SerializeObject(tm);
                context.Response.Write(json);
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "办结状态报表出现的问题！");
            }
        }
    }
}